package com.ljm.security.filter;

import com.ljm.common.constants.SecurityConstants;
import com.ljm.common.constants.SysConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author: ChenHuaMing
 * @Date: 2020/7/3 15:28
 * @Description:
 */
public class HandleCurUserFilter extends GenericFilterBean {
    private static final Logger logger= LoggerFactory.getLogger(HandleCurUserFilter.class);
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
        String loginName=httpServletRequest.getSession(true).getAttribute(SecurityConstants.CURRENT_LOGIN_NAME)!=null?httpServletRequest.getSession().getAttribute(SecurityConstants.CURRENT_LOGIN_NAME).toString():"anonymous";
        SysConstants.LOGIN_NAME_TL.set(loginName);
        logger.info("[{}]-【{}】-request-{}", LocalDateTime.now(),loginName,httpServletRequest.getRequestURI());
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
